//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    longitude: 116.4965075,
    latitude: 40.006103,
    userInfo: {},
    hasUserInfo: false,
    silderBar:[
      {flag:'package',src:'../../images/package.png',content:'我的钱包'},
      {flag:'card',src:'../../images/card.png',content:'我的卡券'},
      {flag:'road',src:'../../images/road.png',content:'我的行程'},
      {flag:'invite',src:'../../images/invite.png',content:'邀请好友'},
      {flag:'myYajin',src:'../../images/trust-point.png',content:'我的押金'},
      {flag:'server',src:'../../images/server.png',content:'客服服务'}
    ],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    open: false,
    // mark 是指原点x轴坐标
    mark: 0,
    // newmark 是指移动的最新点的x轴坐标 
    newmark: 0,
    istoright: true,
    // markers: [{
    //   iconPath: "../../images/location.png",
    //   id: 0,
    //   // bindtap:"back",
    //   latitude: 40.006103,
    //   longitude: 116.4965075,
    //   width: 30,
    //   height: 50
    // }],
    polyline: [{
      points: [{
        longitude: 103.3245211,
        latitude: 23.10229
      }, {
        longitude: 113.324520,
        latitude: 23.21229
      }],
      color: "#FF0000DD",
      width: 2,
      dottedLine: true
    }],
    // controls: [{
    //   id: 1,
    //   iconPath: '../../images/others.png',
    //   position: {
    //     top:100,
    //     bottom:0, 
    //     width: 60,
    //     height: 60
    //   },
    //   clickable: true
    // }]
  },
  // controltap(e) {
    
  // },
  //localtion 按钮
  regionchange(e) {
    // 地图发生变化的时候，获取中间点，也就是用户选择的位置toFixed
    if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
      var that = this;
      this.mapCtx = wx.createMapContext("map4select");
      this.mapCtx.getCenterLocation({
        type: 'gcj02',
        success: function (res) {
          console.log(res, 11111)
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
            circles: [{
              latitude: res.latitude,
              longitude: res.longitude,
              color: '#FF0000DD',
              fillColor: '#d1edff88',
              radius: 3000,//定位点半径
              strokeWidth: 1
            }]
          })
        }
      })
    }
  },
  my_location: function (e) {
    var that = this;
    that.onLoad();
  },

  scan_code:function(){
    wx.scanCode({
      success:(res)=>{
        console.log(res);
      }
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
// 点击事件
  tap_ch: function (e) {
    if (this.data.open) {
      this.setData({
        open: false
      });
    } else {
      this.setData({
        open: true
      });
    }
  },

  tap_start: function (e) {
    // touchstart事件
    // 把手指触摸屏幕的那一个点的 x 轴坐标赋值给 mark 和 newmark
    this.data.mark = this.data.newmark = e.touches[0].pageX;
  },

  tap_drag: function (e) {
  //   // touchmove事件
    this.data.newmark = e.touches[0].pageX;

    },

  //   // 手指从左向右移动
  //   if (this.data.mark < this.data.newmark) {
  //     this.istoright = true;
  //   }

  //   // 手指从右向左移动
  //   if (this.data.mark > this.data.newmark) {
  //     this.istoright = false;
  //   }
  //   this.data.mark = this.data.newmark;
  // },

  tap_end: function (e) {
    // touchend事件
    this.data.mark = 0;
    this.data.newmark = 0;
    // 通过改变 opne 的值，让主页加上滑动的样式
    if (this.istoright) {
      this.setData({
        open: true
      });
    } else {
      this.setData({
        open: false
      });
    }
  },
  //侧滑栏跳转页面
  package:function(){
    wx.navigateTo({
      url: '../packet/packet',
      success: function () {
        console.log('跳转到news页面成功');// success              
      },
      fail: function () {
        console.log('跳转到news页面失败');
      }
    })
  },
  // back:function(){
  //   this.mapCtx.moveToLocation();
  //   console.log('back')
  // }
  card:function(){
    wx.navigateTo({
      url: '../card/card',
      success: function () {
        console.log('跳转到card页面成功');// success              
      },
      fail: function () {
        console.log('跳转到news页面失败');
      }
    })
  },
  road: function () {
    wx.navigateTo({
      url: '../road/road',
      success: function () {
        console.log('跳转到road页面成功');// success              
      },
      fail: function () {
        console.log('跳转到news页面失败');
      }
    })
  },
  invite:()=>{
    wx.navigateTo({
      url: '../invite/invite',
    })
  },

  server: function () {
    wx.navigateTo({
      url: '../service/pages/index/index',
      success: function () {
        console.log('跳转到news页面成功');// success              
      },
      fail: function () {
        console.log('跳转到news页面失败');
      }
    })
  },
  myYajin:function(){
    wx.navigateTo({
      url: '../despoit/despoit',
    })
  },

  bindViewTap: function () {

  },
  onLoad: function () {
    var that = this
    wx.showLoading({
      title: "定位中",
      mask: true
    })
    wx.getLocation({
      type: 'gcj02',
      altitude: true,//高精度定位
      //定位成功，更新定位结果
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy

        that.setData({
          longitude: longitude,
          latitude: latitude,
          speed: speed,
          accuracy: accuracy,
          markers: [{
            iconPath: "../../images/bike-bike.png",
            id: 0,
            // bindtap:"back",
            latitude: latitude - Math.random() * 0.0005,
            longitude: longitude - Math.random() * 0.0005,
            width: 40,
            height: 40
          },
            {
              iconPath: "../../images/bike-bike.png",
              id: 0,
              // bindtap:"back",
              latitude: latitude - Math.random() * 0.0005,
              longitude: longitude - Math.random() * 0.0005,
              width: 40,
              height: 40
            },
            {
              iconPath: "../../images/bike-bike.png",
              id: 0,
              // bindtap:"back",
              latitude: latitude - Math.random() * 0.0005,
              longitude: longitude - Math.random() * 0.0005,
              width: 40,
              height: 40
            },
            {
              iconPath: "../../images/bike-bike.png",
              id: 0,
              // bindtap:"back",
              latitude: latitude - Math.random() * 0.0005,
              longitude: longitude - Math.random() * 0.0005,
              width: 40,
              height: 40
            },
            {
              iconPath: "../../images/bike-bike.png",
              id: 0,
              // bindtap:"back",
              latitude: latitude - Math.random() * 0.0005,
              longitude: longitude - Math.random() * 0.0005,
              width: 40,
              height: 40
            },
            {
              iconPath: "../../images/bike-bike.png",
              id: 0,
              // bindtap:"back",
              latitude: latitude - Math.random() * 0.0005,
              longitude: longitude - Math.random() * 0.0005,
              width: 40,
              height: 40
            }
            ]
        },
        )
      },
      //定位失败回调
      fail: function () {
        wx.showToast({
          title: "定位失败",
          icon: "none"
        })
      },

      complete: function () {
        //隐藏定位中信息进度
        wx.hideLoading()
      }

    })
  },
})
